import {Cell} from "./cell";

// 规格名信息
class Fence {
    specs
    cells = [] // 规格名所包含的一组规格值
    title // 规格名
    id // 规格名id

    constructor(at) {
        this.specs = at
        this.title = at[0].key
        this.id = at[0].key_id
    }

    init() {
        this._initCells()
    }

    setSketchFence(skuList) {
        this.cells.forEach(c=>{
            this. _setCellSkuImg(c, skuList)
        })
    }

    _setCellSkuImg(cell, skuList){
        const cellCode = cell.getCellCode()
        const matchedSku = skuList.find(s=>s.code.includes(cellCode))
        if (matchedSku){
            cell.img = matchedSku.img
        }
    }

    _initCells() {
        this.specs.forEach(spec => {
            // some:有一个元素满足条件则返回true  every:所有元素满足条件则返回true
            if (this.cells.some(c => c.id === spec.value_id)) return // 去重,去除相同的规格值
            const cell = new Cell(spec)
            this.cells.push(cell)
        })
    }

}

export {
    Fence
}
